#include "Railgun.h"

_RailGun::_RailGun() : BaseIndexedGraphicalEntity(), Offset(0.f,0.f,0.f), BoundingBox()
{
	Parent = NULL;
	Effect = NULL;
}

_RailGun::_RailGun(IDirect3DDevice9 *nDevice, int MaxBolts, D3DXVECTOR3 nOffset, ID3DXEffect *nEffect)
	: BaseIndexedGraphicalEntity(nDevice), BoundingBox(nDevice)
{
	Effect = nEffect;
	MaximumBolts = MaxBolts;
	NextBolt = 0;
	LastFireTime = 0;
	Time = 0;
	Parent = NULL;
	Offset = nOffset;
	BoundingBox.InitCollisionBox(0.5f, 0.5f, 3.f);
	CreateCube(D3DXVECTOR3(0.5f, 0.5f, 3.f), D3DCOLOR_ARGB(255, 255, 98, 84));
	Angles = D3DXVECTOR3(0.785398163f,0,0);

	Bolts.reserve(MaximumBolts);
	for(UINT i = 0; i < MaximumBolts; ++i)
	{
		Bolts.push_back(NULL);
	}
	

}

_RailGun::~_RailGun()
{
	for(UINT i = 0; i < Bolts.size(); ++i)
	{
		if(Bolts[i])
		{
			delete Bolts[i];
			Bolts[i] = NULL;
		}
	}
}

void _RailGun::SetParent( Entity *nParent )
{
	Parent = nParent;
}

void _RailGun::SetOffset( D3DXVECTOR3 nOffset )
{
	Offset = nOffset;
}

void _RailGun::Update(float dt)
{
	Time += dt;
	if(Parent)
	{
		D3DXMATRIX RotMat;
		D3DXVECTOR3 RotatedVec;
		D3DXMatrixRotationYawPitchRoll(&RotMat, Angles.x, Angles.y, Angles.z);

		D3DXVec3TransformCoord(&RotatedVec, &Offset, &RotMat);

		Position = Parent->GetPosition() + RotatedVec;
	}
	for(UINT i = 0; i < Bolts.size(); ++i)
	{
		if(Bolts[i])
		{
			if(Bolts[i]->GetIsActive())
				Bolts[i]->Update(dt);
		}
	}
	BaseIndexedGraphicalEntity::Update(dt);
}

void _RailGun::Render( void )
{
	D3DXMATRIX translationMat, rotMat;
	for(UINT i = 0; i < Bolts.size(); ++i)
	{
		if(Bolts[i])
		{
			D3DXMatrixTranslation(&translationMat, Bolts[i]->GetPosition().x, Bolts[i]->GetPosition().y, Bolts[i]->GetPosition().z);
			D3DXMatrixRotationYawPitchRoll(&rotMat, Bolts[i]->GetAngles().x, Bolts[i]->GetAngles().y, Bolts[i]->GetAngles().z);
			Effect->SetMatrix("gWVPMat", &(rotMat *translationMat * WVPMat));
			Effect->CommitChanges();
			Bolts[i]->Render();
		}
	}
	D3DXMatrixTranslation(&translationMat, Position.x, Position.y, Position.z);
	D3DXMatrixRotationYawPitchRoll(&rotMat, Angles.x, Angles.y, Angles.z);
	Effect->SetMatrix("gWVPMat", &(rotMat * translationMat * WVPMat));
	Effect->CommitChanges();
	BaseIndexedGraphicalEntity::Render();
	BoundingBox.Render();
}

void _RailGun::Fire( void )
{
	if((LastFireTime + 1.f) < Time )
	{
		if(NextBolt < MaximumBolts)
		{
			D3DXMATRIX RotMat;
			D3DXVECTOR3 RotatedVec;
			D3DXMatrixRotationYawPitchRoll(&RotMat, Angles.x, Angles.y, Angles.z);

			D3DXVec3TransformCoord(&RotatedVec, &Offset, &RotMat);

			if(Bolts[NextBolt])
				delete Bolts[NextBolt];

			RailBolt *NewBolt = new RailBolt(gd3dDevice, RAIL_SPEED, Angles);
			NewBolt->SetPosition(Position);
			NewBolt->SetAngles(Angles);
			NewBolt->Launch(Time);
			Bolts[NextBolt] = NewBolt;

			Bolts[NextBolt]->Launch(Time);

			NextBolt++;
			LastFireTime = Time;
		}
		else
		{
			if(Bolts[0])
			{
				delete Bolts[0];
				Bolts[0] = NULL;
			}
			NextBolt = 0;
		}
	}
}

void _RailGun::SetWVPMat( D3DXMATRIX nWVPMat)
{
	WVPMat = nWVPMat;
}

void _RailGun::SetAngles(D3DXVECTOR3 nAngles)
{
	Angles = nAngles;
}

D3DXVECTOR3 _RailGun::GetAngles( void )
{
	return Angles;
}